package com.zhengsr.wanandroid.mvp.present;

import com.zhengsr.corelib.bean.NaviData;
import com.zhengsr.corelib.bean.BaseResponse;
import com.zhengsr.corelib.utils.CommonUtils;
import com.zhengsr.corelib.utils.RxUtils;
import com.zhengsr.wanandroid.mvp.base.BasePresent;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.model.DataManager;
import com.zhengsr.wanandroid.widget.CusSubscribe;

import java.util.List;

/**
 * Created by zhengshaorui
 * Time on 2019/3/4
 */

public class NaviPresent extends BasePresent<ContractManager.InaviView> {
    private DataManager mManager;
    private ContractManager.InaviView mView;
    public static NaviPresent create(ContractManager.InaviView iBaseView){
        return new NaviPresent(iBaseView);
    }
    private NaviPresent(ContractManager.InaviView iBaseView){
        mManager = DataManager.getInstance();
        mView = iBaseView;
        iBaseView.showLoading();
        if (CommonUtils.isNetworkConnected()){
            getNaviData();
        }else{
            iBaseView.showError();
        }
    }

    private void getNaviData(){


        addSubscribe(mManager.getNaviData()
               .compose(RxUtils.<BaseResponse<List<NaviData>>>rxScheduers())
                .compose(RxUtils.<List<NaviData>>handleResult())
                .subscribeWith(new CusSubscribe<List<NaviData>>(mView) {
                    @Override
                    public void onNext(List<NaviData> naviData) {

                        mView.loadSuccess();
                        mView.getNaviData(naviData);
                    }
                })

        );
    }
}
